package br.perfiman.exceptions;

import java.util.ArrayList;

/**
 * A Super class for validation errors
 *
 * @author Wagner Costa
 * @since 12/01/2008
 * @version 1.0
 *
 */
public class FieldException extends RuntimeException{
	private static final long serialVersionUID = -88629162356331236L;

	protected ArrayList<FieldError> errors;

	public FieldException() {
		errors = new ArrayList<FieldError>();
	}

	public FieldException(String fieldName, String errorDescription, String... args) {
		this();
		add(fieldName, errorDescription, args);
	}

	/**
	 * Adds a error from validation
	 *
	 * @author Wagner Costa
	 * @since 12/01/2008
	 * @version 1.0
	 * @param fieldName Name of the field
	 * @param errorDescription Description Key from the error
	 */
	public void add(String fieldName, String errorDescription, String... args){
		FieldError error = new FieldError();
		error.fieldName = fieldName;
		error.errorDescription = errorDescription;
		error.args = args;
		errors.add(error);
	}

	public ArrayList<FieldError> getErrors(){
		return errors;
	}

	/**
	 * Indicates if a error was added
	 *
	 * @author Wagner Costa
	 * @since 12/01/2008
	 * @version 1.0
	 * @return <code>true</code> if exists a error, <code>false</code> if not.
	 */
	public boolean hasErrors(){
		return !errors.isEmpty();
	}
}
